//对数组进行了增删操作，不可行
function moveZeroes(nums: number[]): void {
    for (let index = 0; index < nums.length; index++) {
      const element = nums[index];
      if(element === 0) {
        nums.splice(index,1)
        nums.push(element)
      }
    }
    console.log(nums)
};
//想想一下，一个0从头到尾游动，碰到0就黏到一块，然后两个0继续游动，重复操作，一直到最后
function moveZeroes_you_dong(nums: number[]): void {
  let zeroStartIndex = 0//从零开始
  let zeroCount = 0//一共多少个0
  for (let index = 0; index < nums.length; index++) {
    const element = nums[index];
    if(element == 0) {//当前元素为0
      if(zeroCount == 0) {//初始条件
        zeroStartIndex = index
      }
      zeroCount++

    }else {//当前元素不是0
      if(zeroCount != 0) {//把当前元素和0的开头索引交换
        swap(nums,zeroStartIndex,index)
        zeroStartIndex++
      }else {//
        continue
      }
    }
  }
  console.log(nums)
  console.log(zeroStartIndex,zeroCount)
}
//low-fast之间全是0，low可能不是0：fast碰到0就前进一位(是不是0都前进)，low不动。  不是0就交换当前跟low，让0整体前进
function moveZeroes_shuang_zhi_zhen(nums: number[]): void {
  let low = 0
  let fast = 0
  while (fast < nums.length) {
    
    let ele = nums[fast]
    if(ele != 0) {//不等于0
      swap(nums,low,fast)
    }
    if(ele != 0) {//不等于0,交换完，0的整体前进一位
      ++low
    }
    ++fast
    
  }
  console.log(nums)
}
function swap(nums: number[],i:number,j:number): void {
  let a = nums[i]
  nums[i] = nums[j]
  nums[j] = a
}
let array2 = [2,0,3,0,4,0,5,9,7,0]
moveZeroes_shuang_zhi_zhen(array2)